var appTitle = "layui-admx静态管理系统";
//
layui.define(['element','jquery','layer','form'], function(exports){
	var element = layui.element
		  ,$ = layui.jquery
		  ,layer = layui.layer
		  ,form = layui.form;
	app={
	  init:function(homeUrl){
		  var layid = location.hash.replace(/^#navid=/, '');
		  console.log('导航菜单编号',layid);
		  //console.log('导航菜单编号',layid == 'null');
		  if(!/^\d+$/.test(layid)){
			var iHeight = $("#container").height() - 92;
			  $("iframe").first().css({height:iHeight});
		  }else{
			  console.log('自动创建');
			  var node = app.getNav(layid);
			  app.openTab(node.id,node.text,node.url);
		  }
		  //隐藏首页的删除
		  $(".layui-tab ul").children('li').first().children('.layui-tab-close').css("display",'none');
		  //监听选项卡切换
		  element.on('tab(domain)', function(){
			var layid2 = this.getAttribute('lay-id');
			app.autoTabHeight(layid2);
		  });
		  //监听导航菜单点击
		  element.on('nav(navMenu)', function(elem){
		  	console.log(elem.attr('data-id'),elem.attr('data-url'),elem.attr('data-text'));
		  	var id = elem.attr('data-id');
		  	var text = elem.attr('data-text');
		  	var url = elem.attr('data-url');
			if(id === undefined){
				return;
			}
			app.openTab(id,text,url);
		  });
	  },
	  login:function(){
		  console.log(window.title,document.title);
		  layer.open({
			  type: 1
			  ,title: document.title||"  "
			  ,closeBtn: false
			  ,area: '400px;'
			  ,id: 'LAY_login'
			  ,anim:1
			  ,btn: ['登录', '退出']
			  ,btnAlign: 'c'
			  ,content: $('#login')
			  ,btn1: function(layero){
				var username = form.val('loginForm').username;
				var password = form.val('loginForm').password;
				if(username === '' || password === '' ){
					layer.msg('请填写账号或密码', function(){
					  //do something
					});
				}else{
					console.log(form.val('loginForm'));
					layer.load(3); 
				}
			  },
			  btn2:function(index, layero){
			    layer.close();
				location.href="login.html";
			  }
			});
	  },
	  getNav(id){
		var node = {id:'',text:'',url:''};
		if($("ul.layui-nav-tree[lay-filter='navMenu'] a").length > 0){ 
			$.each($("ul.layui-nav-tree[lay-filter='navMenu'] a"),function(index,value){
				if($(this).attr('data-id') === id){
					node.id = $(this).attr('data-id');
					node.text = $(this).attr('data-text');
					node.url = $(this).attr('data-url');
				}
			});
		}
		return node;
	  },
	  autoTabHeight(layid2){
		  var iHeight = $("#container").height() - 92;
		  if(!/^\d+$/.test(layid2)){
		  	$("iframe").first().css({height:iHeight});
		  }else{
		  	$("iframe[id='iframe_"+layid2+"']").first().css({height:iHeight});
		  }
		  location.hash = 'navid='+ layid2;
	  },
	  openTab:function(id,text,url){
		  if(id === ''){
			  app.autoTabHeight(id);
			  return;
		  }
		  location.hash = 'navid='+ id;
		  if($("li[lay-id='"+id+"']").length === 0){
			var iHeight = $("#container").height() - 92;
			element.tabAdd('domain', {
			  title:text
			  ,content: '<iframe id="iframe_'+id+'" src="'+url+'" height = "'+iHeight+'" scrolling="auto" style="width: 100%; border: none;"></iframe>'
			  ,id: id
			});
		  }
		  element.tabChange('domain', id);
	  }
	}
	$(window).resize(function (){
	  var layid2 = location.hash.replace(/^#navid=/, '');
	  app.autoTabHeight(layid2);
	});
	exports('app', app);
});